package com.example.demo.demos.web.controller;

import com.example.demo.demos.web.entity.Car;
import com.example.demo.demos.web.mapper.CarMapper;
import com.example.demo.demos.web.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("index")
public class CarController {
    @Autowired
    private CarMapper carMapper;
    @Autowired
    private CarService carService;
    @GetMapping("/carlist")
    public Map<String,Object> carindex(@RequestParam String carname,@RequestParam Integer pageNum,@RequestParam Integer pageSize){
       Map<String,Object> res=new HashMap<>();
       pageNum=(pageNum-1)*pageSize;
       System.out.println(carname);
       System.out.println(pageNum);
       System.out.println(pageSize);
        List<Car> data=carMapper.selectcar(carname,pageNum,pageSize);
        Integer total=carMapper.totalcar(carname);

        res.put("code",200);
        res.put("data",data);
res.put("total",total);

        return  res;
    }
    @GetMapping("/carbyid")
    public Map<String,Object> selectbyod(@RequestParam String ppid){
     Map<String,Object> res=new HashMap<>();
   List<Car> data=carMapper.selectbyid(ppid);
   res.put("code",200);
   res.put("data",data);

     return res;
    }
    @PostMapping("/carsave")
    public  Map<String,Object> carsave(@RequestBody Car car){
        Map<String,Object> res=new HashMap<>();
        if(car.getId() == null){
            Integer issacve=carService.addcar(car);
            if(issacve==1){
                res.put("code",200);
                res.put("msg","新增成功");

            }else{
                res.put("code",500);
                res.put("msg","新增失败");

            }
        }else{
            Integer issacve=carService.updatecar(car);
            if(issacve==1){
                res.put("code",200);
                res.put("msg","修改成功");

            }else{
                res.put("code",500);
                res.put("msg","修改失败");

            }
        }

        return res;


    }
}
